package com.zby.common.utils;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "返回对象")
public class ResultUtil<T> {
    @Schema(description = "状态码")
    private Integer code;

    @Schema(description = "消息")
    private String msg;

    @Schema(description = "数据")
    private T data;

    private static <T> ResultUtil<T> build(T data) {
        ResultUtil<T> result = new ResultUtil<>();
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static <T> ResultUtil<T> build(T body, ResultEnum resultEnum) {
        ResultUtil<T> result = build(body);
        result.setCode(resultEnum.getCode());
        result.setMsg(resultEnum.getMsg());
        return result;
    }

    // 响应成功
    public static <T> ResultUtil<T> success(T data) {
        return build(data, ResultEnum.SUCCESS);
    }

    public static <T> ResultUtil<T> success() {
        return build(null, ResultEnum.SUCCESS);
    }

    public static <T> ResultUtil<T> fail() {
        return build(null, ResultEnum.FAIL);
    }

    public static <T> ResultUtil<T> fail(String msg) {
        ResultUtil<T> result = build(null, ResultEnum.FAIL);
        result.setMsg(msg);
        return result;
    }
}
